﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Entities;
using System.IO;
using System.Data.SQLite;

namespace WcfServiceLib
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码和配置文件中的类名“Service1”。
    public class Service1 : IService1
    {
        const string databasePath = "database.db";
        public Users Login(string loginID, string password)
        {
            if (!File.Exists(databasePath))
            {
                return null;
            }
            using (SQLiteConnection db = new SQLiteConnection($"Data Source={databasePath};Version=3;"))
            {
                db.Open();

                String tableCommand = $"select * from Users where LoginID='{loginID.ToUpper()}' and Password ='{password}'";

                SQLiteCommand command = new SQLiteCommand(tableCommand, db);
                var reader = command.ExecuteReader();
                if (reader.Read())
                {
                    Users user = new Users();
                    user.ID = reader.GetInt32(0);
                    user.LoginID = reader.GetString(1);
                    user.Password = reader.GetString(2);
                    user.NickName = reader.GetString(3);
                    db.Close();
                    return user;
                }
                else
                {
                    db.Close();
                    return null;
                }
            }

        }

        public bool Regist(string loginID, string password, string nickName)
        {
            if (!File.Exists(databasePath))
            {
                return false;
            }
            using (SQLiteConnection db = new SQLiteConnection($"Data Source={databasePath};Version=3;"))
            {
                db.Open();

                String tableCommand = $"insert into Users(LoginID,Password,NickName)values('{loginID.ToUpper()}','{password}','{nickName}')";

                SQLiteCommand createTable = new SQLiteCommand(tableCommand, db);
                if (createTable.ExecuteNonQuery() > 0)
                {
                    db.Close();
                    return true;
                }
                else
                {
                    db.Close();
                    return false;
                }
            }
        }

        public bool CheckLoginExist(string loginid)
        {
            if (!File.Exists(databasePath))
            {
                return true;
            }
            using (SQLiteConnection db = new SQLiteConnection($"Data Source={databasePath};Version=3;"))
            {
                db.Open();

                String tableCommand = $"select * from Users where LoginID='{loginid.ToUpper()}'";

                SQLiteCommand command = new SQLiteCommand(tableCommand, db);
                var reader = command.ExecuteReader();
                if (reader.Read())
                {
                    db.Close();
                    return true;
                }
                else
                {
                    db.Close();
                    return false;
                }
            }
        }

        public void CreateDatabase()
        {
            // 如果数据库文件不存在，就创建一个新的数据库
            //if (!File.Exists(databasePath))
            //{
            using (SQLiteConnection db = new SQLiteConnection($"Data Source={databasePath};Version=3;"))
            {
                db.Open();

                String tableCommand = @"CREATE TABLE IF NOT 
                        EXISTS Users (ID INTEGER PRIMARY KEY, 
                        LoginID NVARCHAR(20) NULL,
                        Password NVARCHAR(20) NULL,
                        NickName NVARCHAR(20) NULL)";
                SQLiteCommand createTable = new SQLiteCommand(tableCommand, db);
                createTable.ExecuteNonQuery();

                tableCommand = @"CREATE TABLE IF NOT 
                        EXISTS Friends (ID INTEGER PRIMARY KEY,
                        UserID INTEGER NULL, 
                        FriendID INTEGER NULL)";
                createTable = new SQLiteCommand(tableCommand, db);
                createTable.ExecuteNonQuery();
                db.Close();
            }
            //}
        }

        public Dictionary<int, string> GetFriendsList(int id)
        {
            Dictionary<int, string> friends = new Dictionary<int, string>();
            if (!File.Exists(databasePath))
            {
                return friends;
            }
            using (SQLiteConnection db = new SQLiteConnection($"Data Source={databasePath};Version=3;"))
            {
                db.Open();

                String tableCommand = $"select Friends.FriendID,Users.NickName from Friends left join Users on Users.ID = Friends.FriendID where Friends.UserID={id}";

                SQLiteCommand command = new SQLiteCommand(tableCommand, db);
                var reader = command.ExecuteReader();
                while (reader.Read())
                {
                    friends.Add(reader.GetInt32(0), reader.GetString(1));
                }
                db.Close();
            }
            return friends;
        }

        public Users GetUsers(int id)
        {
            if (!File.Exists(databasePath))
            {
                return null;
            }
            using (SQLiteConnection db = new SQLiteConnection($"Data Source={databasePath};Version=3;"))
            {
                db.Open();

                String tableCommand = $"select * from Users where ID={id}";

                SQLiteCommand command = new SQLiteCommand(tableCommand, db);
                var reader = command.ExecuteReader();
                if (reader.Read())
                {
                    Users user = new Users();
                    user.ID = reader.GetInt32(0);
                    user.LoginID = reader.GetString(1);
                    user.Password = reader.GetString(2);
                    user.NickName = reader.GetString(3);
                    db.Close();
                    return user;
                }
                else
                {
                    db.Close();
                    return null;
                }
            }
        }

        public bool AddFriend(int myid, string myname, int friendID, string friendname)
        {
            if (!File.Exists(databasePath))
            {
                return false;
            }
            try
            {
                using (SQLiteConnection db = new SQLiteConnection($"Data Source={databasePath};Version=3;"))
                {
                    db.Open();
                    string tableCommand = $"INSERT OR IGNORE INTO Friends(UserID,FriendID)values(@myid,@fid)";
                    SQLiteCommand command = new SQLiteCommand(tableCommand, db);
                    command.Parameters.Add(new SQLiteParameter("@myid", myid));
                    command.Parameters.Add(new SQLiteParameter("@fid", friendID));
                    command.ExecuteNonQuery();

                }
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

        public bool EditNickName(int id, string nickName)
        {
            if (!File.Exists(databasePath))
            {
                return false;
            }
            try
            {
                using (SQLiteConnection db = new SQLiteConnection($"Data Source={databasePath};Version=3;"))
                {
                    db.Open();
                    string tableCommand = $"update Users set NickName=@nickName where ID=@id";
                    SQLiteCommand command = new SQLiteCommand(tableCommand, db);
                    command.Parameters.Add(new SQLiteParameter("@nickName", nickName));
                    command.Parameters.Add(new SQLiteParameter("@id", id));
                    command.ExecuteNonQuery();

                }
            }
            catch (Exception ex)
            {
                return false;
            }

            return true;
        }

    }
}
